package com.atguigu.controller;

import com.atguigu.base.BaseController;
import com.atguigu.en.DictCodeEnum;
import com.atguigu.entity.Community;
import com.atguigu.entity.Dict;
import com.atguigu.service.CommunityService;
import com.atguigu.service.DictService;
import com.github.pagehelper.PageInfo;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/community")
public class CommunityController extends BaseController {

    @DubboReference
    private DictService dictService;

    @DubboReference
    private CommunityService communityService;

    //小区管理列表展现页面
    //分页和搜索结合实现跳转到小区管理页面
    @RequestMapping
    public String index(@RequestParam Map<String,Object> filters, Model model) {
        //1.调用业务层的方法查询小区的分页信息，并存储到请求域
        PageInfo<Community> page = communityService.findPage(filters);
        model.addAttribute("page", page);
        //2.将搜索条件存储到请求域
        if (ObjectUtils.isEmpty(filters.get("areaId"))) {
            filters.put("areaId", 0);
        }
        if (ObjectUtils.isEmpty(filters.get("plateId"))) {
            filters.put("plateId", 0);
        }
        model.addAttribute("filters", filters);
        //3.调用业务层的方法查询北京的所有区域，并存储到请求域
        List<Dict> areaList = dictService.findListByParentDictCode(DictCodeEnum.BEIJING.getCode());
        model.addAttribute("areaList", areaList);
        //4.显示小区的首页
        return "community/index";
    }

    //方式二
    //分页和搜索结合实现跳转到小区管理页面
   /* @RequestMapping
    public String index(ModelMap model, HttpServletRequest request) {
        Map<String,Object> filters = getFilters(request);

        PageInfo<Community> page = communityService.findPage(filters);

        List<Dict> areaList = dictService.findListByDictCode("beijing");
        model.addAttribute("areaList",areaList);

        model.addAttribute("page", page);
        model.addAttribute("filters", filters);
        return "community/index";
    }*/

    //跳转到小区新增页面
    @GetMapping("/create")
    public String create(Model model) {
        //查询北京的所有区域，存储到请求域
        List<Dict> areaList = dictService.findListByParentDictCode(DictCodeEnum.BEIJING.getCode());
        model.addAttribute("areaList", areaList);
        //显示小区的新增页面
        return "community/create";
    }

    //实现小区新增
    @PostMapping("/save")
    public String save(Community community) {
        //1.调用业务层方法保存数据
        communityService.insert(community);
        //2.显示成功数据
        return "common/successPage";
    }

    /**
     * 修改小区信息
     * (1)根据id查询要修改的小区信息
     * (2)回显
     * (3)进行修改
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable Long id, Model model) {
        //调用业务方法根据id查询小区信息，并存储到请求域
        Community community = communityService.getById(id);
        model.addAttribute("community", community);
        //查询北京的所有区域，存储到请求域
        List<Dict> dictList = dictService.findListByParentDictCode(DictCodeEnum.BEIJING.getCode());
        model.addAttribute("areaList", dictList);
        return "community/edit";
    }

    @PostMapping("/update")
    public String update(Community community) {
        communityService.update(community);
        return "common/successPage";
    }

    //删除小区信息
    @GetMapping("/delete/{id}")
    public String delet(@PathVariable Long id) {
        communityService.delete(id);
        //重定向到小区首页
        return "redirect:/community";
    }


}
